package com.longdaji.decoration.ui.pay;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.data.bean.PakageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longtao on 2018/1/31.
 */

public class PakageAdapter extends RecyclerView.Adapter<PakageAdapter.ViewHolder> {

    private List<PakageBean> mPakageList = new ArrayList<>();


    public PakageAdapter(List<PakageBean> list) {
        if (list != null) {
            this.mPakageList = list;
        }
    }

    public void setData(List<PakageBean> list) {
        if (list != null) {
            this.mPakageList = list;
        }
        notifyDataSetChanged();
    }

    public PakageBean getSelectItem() {
        for (PakageBean pakageBean : mPakageList) {
            if (pakageBean.isSelect()) {
                return pakageBean;
            }
        }
        return null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pakage, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final PakageBean pakageItem = mPakageList.get(position);
        if (pakageItem.getName() != null) {
            holder.name.setText(pakageItem.getName());
        }

        holder.money.setText("¥" + pakageItem.getPackageMoney());

        if (pakageItem.isSelect()) {
            holder.isSelect.setChecked(true);
        } else {
            holder.isSelect.setChecked(false);
        }

        holder.pakageItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pakageItem.setSelect(true);
                for (PakageBean pakageBean : mPakageList) {
                    if (!TextUtils.equals(pakageBean.getName(), pakageItem.getName())) {
                        pakageBean.setSelect(false);
                    }
                }
                notifyDataSetChanged();

            }
        });


    }

    @Override
    public int getItemCount() {
        return mPakageList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private TextView money;
        private LinearLayout pakageItem;
        private CheckBox isSelect;

        public ViewHolder(View itemView) {
            super(itemView);
            pakageItem = itemView.findViewById(R.id.llyt_pakage_item);
            name = itemView.findViewById(R.id.tv_pakage_name);
            money = itemView.findViewById(R.id.tv_pakage_money);
            isSelect = itemView.findViewById(R.id.cb_status);
        }
    }

}
